package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import service.CustomUserDetailService;
import bean.CustomUser;


@Controller
public class HomeController {
	
	@Autowired
	CustomUserDetailService userDetailService;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "home";
    }
	
	
	@RequestMapping(value = "/hello**", method = RequestMethod.GET)
    public ModelAndView hello() {
		ModelAndView model = new ModelAndView();
		model.addObject("title", "Spring Security Custom Login Form");
		model.addObject("message", "This is protected page!");
		model.setViewName("hello");

		return model;
    }
	
	@PreAuthorize("hasRole('USER')")
    @RequestMapping(value = "/userList/", method = RequestMethod.POST)
    public ResponseEntity<List<UserDetails>> listAllUsers() {
		UserDetails loadUserByUsername = userDetailService.loadUserByUsername("");
		List<UserDetails> list = new ArrayList<UserDetails>();
		list.add(loadUserByUsername);
        return new ResponseEntity<List<UserDetails>>(list, HttpStatus.OK);
    }
	 
	@RequestMapping(value = "/oauthhello**", method = RequestMethod.GET)
    public ModelAndView oauthHello() {
		ModelAndView model = new ModelAndView();
		model.addObject("title", "Spring Security Custom Login Form");
		model.addObject("message", "This is protected page!");
		model.setViewName("oauthhello");

		return model;
    }
	//Spring Security see this :
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(
		@RequestParam(value = "error", required = false) String error,
		@RequestParam(value = "logout", required = false) String logout) {

		ModelAndView model = new ModelAndView();
		if (error != null) {
			model.addObject("error", "Invalid username and password!");
		}
		if (logout != null) {
			model.addObject("msg", "You've been logged out successfully.");
		}
		model.setViewName("login");
		return model;

	}

/*	@RequestMapping(value="/logout", method = RequestMethod.GET)
	public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if (auth != null){    
			new SecurityContextLogoutHandler().logout(request, response, auth);
		}
		return "redirect:/login?logout";//You can redirect wherever you want, but generally it's a good practice to show login screen again.
	}*/

 /*  @RequestMapping(value = "/userList/", method = RequestMethod.GET)
    public ResponseEntity<List<CustomUser>> getUserList(@PathVariable("id") long id) {
        
	    List<CustomUser> userList = new ArrayList<CustomUser>();
		
	    userList.add(new CustomUser(Long.valueOf(1), "chen1", "111111", true));
	    userList.add(new CustomUser(Long.valueOf(1), "chen2", "222222", true));
	    userList.add(new CustomUser(Long.valueOf(1), "chen3", "333333", true));
	    userList.add(new CustomUser(Long.valueOf(1), "chen4", "444444", true));

        if (userList == null) {
            System.out.println("User with id " + id + " not found");
            return new ResponseEntity<List<CustomUser>>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<List<CustomUser>>(userList, HttpStatus.OK);
    }*/
	
}
